package com.csdemos;

import com.csdemos.algs.ArrayUtils;
import com.csdemos.algs.SortUtils;
import com.csdemos.R;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class SortActivity extends Activity {
	
	public static final String SORT_METHOD = "sort_method";
	public static final String SORT_METHOD_TITLE = "sort_method_title";
	public static final int SORT_METHOD_TITLE_UNKNOWN = -1;
	
	private static final int ITEMS_COUNT = 10;
	private static final int MAX_VALUE = 50;
	
	private EditText arrayEditText;
	private TextView resultTextView;
	private int sortMethod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int titleId = getIntent().getIntExtra(SORT_METHOD_TITLE, SORT_METHOD_TITLE_UNKNOWN);
		sortMethod = getIntent().getIntExtra(SORT_METHOD, SortUtils.NO_SORT);
		setTitle(titleId);
		setContentView(R.layout.activity_sort);
		arrayEditText = (EditText) findViewById(R.id.array);
		resultTextView = (TextView) findViewById(R.id.result);
		TextView kTextView = (TextView) findViewById(R.id.max_value);
		TextView sizeTextView = (TextView) findViewById(R.id.size);
		
		kTextView.setText(String.format(getString(R.string.k), MAX_VALUE));
		sizeTextView.setText(String.format(getString(R.string.size), ITEMS_COUNT));
	}
	
	public void randomize(View view) {
		arrayEditText.setText(ArrayUtils.arrayToString(
				ArrayUtils.generateRandomArray(ITEMS_COUNT, MAX_VALUE)));
	}
	
	public void sort(View view) {
		int[] array = ArrayUtils.stringToArray(arrayEditText.getText().toString());
		SortUtils.sort(array, MAX_VALUE, sortMethod);
		resultTextView.setText(ArrayUtils.arrayToString(array));
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_quick_sort, menu);
		return true;
	}

}
